//
//  AppDelegate.swift
//  WWDCLearn
//
//  Created by dev on 2021/6/28.
//

import Cocoa
import os.log
import Sparkle
@_exported import ConfUIFoundation

extension Notification.Name {
    static let openWWDCURL = Notification.Name(rawValue: "OpenWWDCURLNotification")
}


@main
class AppDelegate: NSObject, NSApplicationDelegate {

    private var storage: Storage?
    private var syncEngine: SyncEngine?
   private lazy var agentController = WWDCAgentController()

    private let log = OSLog(subsystem: "io.wwdc.app", category: String(describing: AppDelegate.self))

    private lazy var commandsReceiver = AppCommandsReceiver()

    private(set) var coordinator:AppCoordinator? {
        didSet {
            if coordinator != nil {
              openPendingDeepLinkIfNeeded()
            }
        }
    }

    private let boot = Boot()
    
    private var pendingDeepLink: DeepLink?

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application
        UserDefaults.standard.register(defaults: ["NSApplicationCrashOnExceptions":false])
//        NSApp.registerForRemoteNotifications(matching: [])

        boot.bootstrapDependencies { [unowned self] result in
            switch result {
            case .failure(let error)
            handleBootstrapError(error)
            case .success(let dependencies):

            }
        }
    }

    func startupUI(using storage: Storage, syncEngine:SyncEngine , force: Bool = false)  {
        self.storage = storage
        self.syncEngine = syncEngine
//         coordinator = AppCoordinator(w)
    
    }

  private   func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }
    
    private func openPendingDeepLinkIfNeeded(){
        guard let deepLink = pendingDeepLink else { return  }
        
    }

    func handleBootstrapError(_ error: Boot.BootstrapError)  {

    }

    @objc func handleURLEvent(_ event:NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?){
        guard let event = event else { return  }
        guard let urlString = event.paramDescriptor(forKeyword: UInt32(keyDirectObject))?.stringValue else { return  }
        guard let url = URL(string: urlString) else { return  }
         openURL(url)
    }
    private func openURL(_ url:URL){
        if let command = WWDCAppCommand(from: url) {

        }

    }
    private func activateUI(){


    }
    private func openPendingDeepLinkIfNeeded(){
        
    }
}



extension AppDelegate :SUUpdaterDelegate {
    func updaterMayCheck(forUpdates updater: SUUpdater) -> Bool {
        #if DEBUG
        return ProcessInfo.processInfo.arguments.contains("--enable-update")
        #else
        return true
        #endif
    }
}

extension AppDelegate {
    func handle(_ command:WWDCAppCommand)  {
        if command.isForeground {
             activateUI()
            DispatchQueue.main.async {
                NSApp.activate(ignoringOtherApps: true)
            }
        }
    }
}
